﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using debioscoopincdrie.Managers;

namespace debioscoopincdrie.Model
{
    class ReservationChecker
    {
        private Boolean doStuff = false;

        private DateTime lastUpdate;

        public void start()
        {
            doStuff = true;
            lastUpdate = DateTime.Now.AddMinutes(-1); //Do the first update right now

            while ( doStuff )
            {
                if (DateTime.Now.AddMinutes(-1) >= lastUpdate)
                {
                    Console.WriteLine("Checking today's reservations...");
                    foreach (bestelling reservation in ReservationManager.GetManager().getReservations())
                    {
                        if (reservation.soort.Equals(Enums.OrderType.reservering.ToString()))
                        {
                            if (reservation.active && reservation.datum == DateTime.Today)
                            {
                                Console.WriteLine("found one");
                                if (DateTime.Now.AddMinutes(-30).TimeOfDay >= reservation.tijd)
                                {
                                    Console.WriteLine("30 minutes before movie, delete reservation...");
                                    reservation.active = false;
                                    DatabaseManager.GetManager().GetEntities().SaveChanges();
                                }
                            }
                        }
                    }
                    lastUpdate = DateTime.Now;
                }
            }
        }

        public void stop()
        {
            doStuff = false;
        }
    }
}
